//
//  MainAccountCell.swift
//  d11
//
//  Created by peale on 2018/9/17.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias AccountOutOrInBlock = (_ index: Int) -> Void
class MainAccountCell: UITableViewCell {
     var accountOutOrInBlock: AccountOutOrInBlock?
    func callClickBlock(_ block: @escaping AccountOutOrInBlock) {
        
        accountOutOrInBlock = block
    }
    var bgImgView : UIImageView?
    var titleLabel : UILabel?
     var subTileLabel : UILabel?
    var  intoBtn :UIButton?
    var lineView : UIView?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.clear
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.bgImgView = UIImageView.init()
        self.bgImgView?.isUserInteractionEnabled = true
        self.bgImgView?.image = imageNamed("icon_my_cell_center")
        self.contentView.addSubview(bgImgView!);
        self.bgImgView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-10)
            make.height.equalTo(51)
            make.centerX.equalTo(self.contentView)
        }
        
        
        self.titleLabel = UILabel.init()
        self.titleLabel?.textColor = HexRGBAlpha(0x333333, 1)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        self.titleLabel?.textAlignment = NSTextAlignment.left
         self.titleLabel?.numberOfLines = 2
        //此处的0.7是缩小到0.7倍就不缩小了，如果还是超过那么就省略显示多余部分
        self.titleLabel?.adjustsFontSizeToFitWidth=true
        self.titleLabel?.minimumScaleFactor = 0.9
        self.bgImgView?.addSubview(self.titleLabel!)
        let titleW = (kScreenWidth-25-90)/2
        self.titleLabel?.snp.makeConstraints{(make) in
            make.left.equalTo(20)
            make.width.equalTo(titleW)
            make.height.equalTo(51)
            make.centerY.equalToSuperview()
        }
        
        self.subTileLabel = UILabel.init()
        self.subTileLabel?.textColor = UIColor.red
        self.subTileLabel?.font = UIFont.systemFont(ofSize: 13)
        self.subTileLabel?.textAlignment = NSTextAlignment.left
        self.bgImgView?.addSubview(self.subTileLabel!)
        
        self.subTileLabel?.snp.makeConstraints{(make) in
            make.left.equalTo((self.bgImgView?.snp.centerX)!)
           // make.width.equalTo(100)
            make.centerY.equalToSuperview()
            make.height.equalTo(25)
        }
        
        self.intoBtn = UIButton.init(type: .custom)
        self.intoBtn?.layer.masksToBounds = true
        self.intoBtn?.layer.cornerRadius = 5
        self.intoBtn?.backgroundColor = UIColor.white
        self.intoBtn?.layer.borderWidth = 1
        self.intoBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        self.intoBtn?.layer.borderColor = HexRGBAlpha(0xFCD199, 1).cgColor
        self.intoBtn?.setTitleColor(HexRGBAlpha(0xFCD199, 1),for: .normal)
        self.intoBtn?.setTitle("一键转入", for: .normal)
        self.intoBtn?.addTarget(self, action: #selector(self.actionToClick(_:)), for: .touchUpInside)
        self.bgImgView?.addSubview(self.intoBtn!)
        
        self.intoBtn?.snp.makeConstraints{(make) in
            make.right.equalToSuperview().offset(-15)
            make.width.equalTo(70)
            make.centerY.equalToSuperview()
            make.height.equalTo(25)
        }
        
        self.lineView = UIView.init()
        self.lineView?.backgroundColor = HexRGBAlpha(0xF7F8F8, 1)
        self.bgImgView?.addSubview(self.lineView!)
        
        self.lineView?.snp.makeConstraints{(make) in
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().offset(-35)
            make.height.equalTo(0.7)
            make.bottom.equalToSuperview()
        }
    }
    func setData(dict:[String:Any]?,index:Int) -> Void {
        self.titleLabel?.text = dict?["name"] as? String
        self.intoBtn?.tag = index
        if dict!["id"] as? String == "10000" {
            self.intoBtn?.setTitle("一键回收", for: .normal)
        }else{
            self.intoBtn?.setTitle("一键转入", for: .normal)
        }
        if dict?["val"] == nil || dict?["status"] == nil {
            self.subTileLabel?.text = "读取中..."
            self.subTileLabel?.textColor = HexRGBAlpha(0xBBBEBF, 1)
            self.intoBtn?.layer.borderColor = HexRGBAlpha(0xBBBEBF, 1).cgColor
            self.intoBtn?.setTitleColor(HexRGBAlpha(0xBBBEBF, 1),for: .normal)
            self.subTileLabel?.snp.remakeConstraints{(make) in
                make.centerY.equalToSuperview()
                make.height.equalTo(25)
                make.centerX.equalTo((self.bgImgView?.snp.centerX)!)
            }
        }else{
            self.subTileLabel?.text = dict?["val"] as? String
            let status :Int = dict?["status"]  as? Int ?? -1
            if status == 0 || status == 2{ //维护中 /读取失败!
                self.subTileLabel?.textColor = HexRGBAlpha(0xBBBEBF, 1)
                self.intoBtn?.layer.borderColor = HexRGBAlpha(0xBBBEBF, 1).cgColor
                self.intoBtn?.setTitleColor(HexRGBAlpha(0xBBBEBF, 1),for: .normal)
            }else{
                self.subTileLabel?.textColor = UIColor.red
                self.intoBtn?.layer.borderColor = HexRGBAlpha(0xFCD199, 1).cgColor
                self.intoBtn?.setTitleColor(HexRGBAlpha(0xFCD199, 1),for: .normal)
            }
            self.subTileLabel?.snp.remakeConstraints{(make) in
                if (self.subTileLabel?.text?.count)! >= 6{
                    make.centerY.equalToSuperview()
                    make.height.equalTo(25)
                    make.left.equalTo((self.bgImgView?.snp.centerX)!).offset(-20)
                }else{
                    make.centerY.equalToSuperview()
                    make.height.equalTo(25)
                    make.centerX.equalTo((self.bgImgView?.snp.centerX)!)
                }
            }
        }
    }
    
    @objc func actionToClick(_ sender: UIButton) -> Void {
        if accountOutOrInBlock != nil {
            accountOutOrInBlock!(sender.tag)
        }
    }
}
